require "import"
import "android.widget.*"
import "com.androlua.*"

local buttonList = {
    "Canción anterior",
    "Reproducir/Pausar",
    "Siguiente canción",
    "Bloqueo de diálogo"
}

local layout = {
    LinearLayout,
    orientation = "vertical",
    layout_width = "fill",
    layout_height = "fill",
    {
        TextView,
        text = "Controles de música",
        textSize = "20sp",
        gravity = "center",
        layout_width = "fill",
        layout_height = "wrap"
    },
    {
        GridView,
        id = "grid",
        numColumns = 1,
        layout_width = "fill",
        layout_height = "fill"
    }
}

local dlg = LuaDialog(service)
dlg.setView(loadlayout(layout))
grid.adapter = ArrayAdapter(service, android.R.layout.simple_list_item_1, buttonList)
dlg.setButton("Cancelar", nil)
dlg.show()

local dialogLocked = false

grid.onItemClick = function(l, v, p, i)
    if i == 0 then
        if not dialogLocked then
            dlg.dismiss()
        end
        if service.execute("Pista anterior") then
            return true
        end
    elseif i == 1 then
        if not dialogLocked then
            dlg.dismiss()
        end
        if service.execute("Pausar la reproducción") then
            return true
        end
    elseif i == 2 then
        if not dialogLocked then
            dlg.dismiss()
        end
        if service.execute("Pista siguiente") then
            return true
        end
    elseif i == 3 then
        dialogLocked = not dialogLocked
        if dialogLocked then
            service.asyncSpeak("Bloqueo de diálogo activado")
        else
            service.asyncSpeak("Bloqueo de diálogo desactivado")
        end
    end
end

return true
